<!DOCTYPE html>
<html lang="zh">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta charset="utf-8"/>
    <title>title</title>

</head>
<body>
    <div id="app">
        <parent></parent>
    </div>

    <script type="text/javascript" src="../vue-2.5.17/dist/vue.js"></script>
    <script>
        Vue.component('parent', {
            template: `
                <div>
                    <p>{{message}}</p>
                    <child @change="childDataChange" ref="child"></child>
                </div>
                
            `,
            data: function() {
                return {
                    name: 'parent',
                    message: 'click button!'
                }
            },
            methods: {
                childDataChange: function() {
                    console.log(this, this.name);
                    this.message = arguments;
                }
            },
            created: function() {
                this.$nextTick(() => {
                    this.$refs.child.$on('change', function() {
                        console.log(this, this.name);
                    })    
                })
                
            }
        });
        Vue.component('child', {
            template: `<button @click="changeData">random</button>`,
            data: function() {
                return {
                    name: 'child'
                }
            },
            methods: {
                changeData: function() {
                    this.$emit('change',
                        Math.floor(Math.random() * 1000),
                        Math.floor(Math.random() * 1000) + 1000
                    );
                }
            }
        });
        new Vue({
            el: "#app"
        });
    </script>
</body>
</html>